package com.dy.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.dy.dto.UserDTO;
import com.dy.dto.UserLoginDTO;
import com.dy.service.UserService;
import com.dy.utils.ApiResult;
import com.dy.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @Author: dy
 * @date: 2024/5/7 10:04
 * @ClassDescription: 用户管理
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/register")
    @ApiOperation("注册")
    public ApiResult<Boolean> register(@Valid @RequestBody UserDTO dto){
        return ApiResult.ok(userService.register(dto));
    }

    @PostMapping("/login")
    @ApiOperation("登录")
    public ApiResult<UserVO> login(@Valid @RequestBody UserLoginDTO dto){
        return ApiResult.ok(userService.login(dto));
    }

    @GetMapping("/loginOut")
    @ApiOperation("退出登录")
    public ApiResult<Boolean> login(){
        String tokenValue = StpUtil.getTokenValue();
        StpUtil.logoutByTokenValue(tokenValue);
        return ApiResult.ok();
    }


}
